/* Copyright 2007-2008 dnAnalytics Project.
 *
 * Contributors to this file:
 * Patrick van der Velde
 * Marcus Cuda
 * Jurgen Van Gael
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * * Redistributions of source code must retain the above copyright notice, this 
 *   list of conditions and the following disclaimer.
 * * Redistributions in binary form must reproduce the above copyright notice, 
 *   this list of conditions and the following disclaimer in the documentation
 *   and/or other materials provided with the distribution.
 * * Neither the name of the dnAnalytics Project nor the names of its contributors
 *   may be used to endorse or promote products derived from this software without
 *   specific prior written permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */
using System;
using System.IO;

namespace dnAnalytics.LinearAlgebra.IO
{
    /// <summary>
    /// Writes an <see cref="Matrix"/> to delimited text file. If the user does not
    /// specify a delimiter, a tab separator is used.
    /// </summary>
    public class DelimitedMatrixWriter : SingleMatrixWriter
    {

        private readonly string delimiter;

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using a comma as the delimiter.
        /// </summary>
        public DelimitedMatrixWriter()
        {
            delimiter = "\t";
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(char delimiter)
        {
            this.delimiter = new string(delimiter, 1);
        }

        /// <summary>
        /// Constructs a <b>DelimitedMatrixWriter</b> using the given delimiter.
        /// </summary>
        /// <param name="delimiter">the delimiter to use.</param>
        public DelimitedMatrixWriter(string delimiter)
        {
            this.delimiter = delimiter;
        }

        /// <summary>
        /// Writes the given <see cref="Matrix"/> to the given <b>TextWriter</b>.
        /// </summary>
        /// <param name="matrix">The matrix to write.</param>
        /// <param name="writer">The <see cref="TextWriter"/> to write the matrix to.</param>
        /// <param name="format">The format to use on each element.</param>
        /// <exception cref="ArgumentNullException">If either <paramref name="matrix"/> or <paramref name="writer"/> is null.</exception>
        protected override void DoWriteMatrix(Matrix matrix, TextWriter writer, string format)
        {
            if (matrix == null)
            {
                throw new ArgumentNullException("matrix");
            }
            if (writer == null)
            {
                throw new ArgumentNullException("writer");
            }

            int cols = matrix.Columns - 1;
            int rows = matrix.Rows - 1;
            for (int i = 0; i < matrix.Rows; i++)
            {
                for (int j = 0; j < matrix.Columns; j++)
                {
                    writer.Write(matrix[i, j].ToString(format, System.Globalization.CultureInfo.InvariantCulture));
                    if (j != cols)
                    {
                        writer.Write(delimiter);
                    }
                }
                if (i != rows)
                {
                    writer.Write(Environment.NewLine);
                }
            }
        }
    }
}
